//==================================================================================
//		Dual Quad Board Program
//		file name : Initial_MCU.H
//		version : 1.0
//		target device : w78e516(winbond)
//		target board : dual quad board
//		c compiler : Keil
//==================================================================================

void Init_Port();
void Init_Resetn();
void Init_Interrupt();

//... w78e516 port define
//---------------------------------------------------------
//... tw2824 control port
#define	MDATA			P0		//... tw2824  MDATA(7:0)
#define	HWRB			P1_0	//... tw2824 control pin HWRB
#define	HRDB			P1_1	//... tw2824 control pin HRDB
#define	HALE			P1_2	//... tw2824 control pin HALE
#define	HCSB0			P1_3	//... tw2824 control pin HCSB0
#define	HCSB1			P1_4	//... tw2824 control pin HCSB1
#define	PB_SEL_0		P1_6	//... VCR Input Select TW9903 or TW2824
#define	PB_SEL_1		P1_7	//... Select 4 Channel or 8 Channel 
#define	VSENC			P3_3	//...TW2824 VSENC

//... alarm, key, led control port
#define	LED_LE			P2_0	//... led LE
#define	KEY_OE			P2_1	//... key OE
#define	chip_sel_LE		P2_2	//... chip select OE
#define	BUZZER_OUT		P2_3	//... buzzer out
#define	RELAY_OUT		P3_7	//... relay out

//... rs-232 control port
#define	RXD_232		P3_0	//... rs232 RXD
#define	TXD_232		P3_1	//... rs232 TXD

//... i2c control port (tw9903, rtc, eeprom)
#define	I2C_SDA_EEPROM 	P2_4	//... EEPROM I2C_SCL
#define	I2C_SCL_EEPROM 	P2_5	//... EEPROM I2C_SDA
#define	I2C_SDA			P2_6	//... I2C_SDA
#define	I2C_SCL				P2_7	//... I2C_SCL

#define	RTC_SQW			P3_5	//... rtc SQW

//... mouse control port
#define	MOUSE_CLK			P3_2	//... mouse CLK
#define	MOUSE_DATA		P3_4	//... mouse DATA

//... reset control port
#define	RESETn				P1_5	//... tw2824 , tw9903  RESETn
//---------------------------------------------------------

//... i2c slave id define
//---------------------------------------------------------
//... eeprom
#define rom_p0		0xa0		//... bank0
#define rom_p1		0xa2		//... bank1
#define rom_p2		0xa4		//... bank2
#define rom_p3		0xa6		//... bank3

//... rtc
#define rtc_sadd		0xd0

//...pvx522
#ifdef VGA_DISPLAY
#ifdef pvx522
#define	VGA_I2C_ADD	(0x2e)	
#endif
#endif
//---------------------------------------------------------

code U8 key_scan_data[] = {
	0xfe,0x01,    0xfd,0x02,
	0xfb,0x03,    0xf7,0x04,
	0xef,0x05,    0xdf,0x06,
	0xbf,0x07,    0x7f,0x08,
	0x76,0x88
};
/*
code U8 remote_scan_data[] = {
	0x90,MENU_KEY,    	0x98,ZOOM_KEY,
	0x88,ENTER_KEY,    	0xf8,UP_KEY,
	0x68,DOWN_KEY,    	0xd8,LEFT_KEY,
	0xa8,RIGHT_KEY,    	0xe8,FREEZE_KEY,
	0x02,PSEUDO_16CH_KEY,    		0x32,QUAD_KEY,
	0x20,B_KEY,	    		0x00,AUTO_KEY,	
	0x50,REALTIME_8CH_KEY_A,    	0x78,MODE_KEY,
	0x70,R_PIP1_KEY,    	0x58,R_SP_2_KEY,
	0x38,CH1_KEY,    		0x28,CH2_KEY,
	0xf0,CH3_KEY,    		0x30,CH4_KEY
};*/
//for junanan
code U8 remote_scan_data[] = {
	0x08,MENU_KEY,    	0x90,ZOOM_KEY,
	0xf8,ENTER_KEY,    	0x02,UP_KEY,
	0xe8,DOWN_KEY,    	0xb8,LEFT_KEY,
	0x98,RIGHT_KEY,    	0xd8,FREEZE_KEY,
	0x48,AUTO_KEY,   		0x68,MODE_KEY,
	0x12,QUAD_KEY,		0x20,REALTIME_8CH_KEY_A, 
//	0xa0,PSEUDO_16CH_KEY, 
	0x78,CH1_KEY,  	
	0x50,CH2_KEY,    		0x32,CH3_KEY,
	0x30,CH4_KEY,    		0x58,CH5_KEY,
	0xa8,CH6_KEY,    		0x2a,CH7_KEY,
	0xb0,CH8_KEY
};

// for shenshiyin
/*code U8 remote_scan_data[] = {
	0x08,MENU_KEY,    	0x90,ZOOM_KEY,
	0xf8,ENTER_KEY,    	0x80,UP_KEY,
	0x88,DOWN_KEY,    	0xb8,LEFT_KEY,
	0xb0,RIGHT_KEY,    	0x98,FREEZE_KEY,
	0xa8,AUTO_KEY,   		0xe8,MODE_KEY,
	0x48,QUAD_KEY,		0x68,REALTIME_8CH_KEY_A, 
	0x02,CH1_KEY,  		0x32,CH2_KEY,
	0x20,CH3_KEY,		0x00,CH4_KEY,
	0x50,CH5_KEY,		0x78,CH6_KEY,
	0x70,CH7_KEY,		0x58,CH8_KEY  		
};
*/
/*code U8 serial_scan_data[] = {
	0x8e,MENU_KEY,    	0x8d,ZOOM_KEY,
	0x93,ENTER_KEY,    	0x8f,UP_KEY,
	0x90,DOWN_KEY,    	0x91,LEFT_KEY,
	0x92,RIGHT_KEY,    	0x8c,FREEZE_KEY,
	0x8b,VCR_KEY,    		0x84,A_KEY,
	0x85,B_KEY,	    		0x86,AUTO_KEY,	
	0x87,R_8CH_KEY,    	0x88,R_PIP2_KEY,
	0x89,R_PIP1_KEY,    	0x8a,R_SP_2_KEY,
	0x80,CH1_KEY,    		0x81,CH2_KEY,
	0x82,CH3_KEY,    		0x83,CH4_KEY
};*/
//==================================================================================

//==================================================================================
//... initialize port
void Init_Port()
{
	P0 = 0xff;
	P1 = 0xff;
	P2 = 0xff;
	P3 = 0xff;
	P4 = 0x0f;
}
//==================================================================================
void Init_Resetn()
{
	U16 t1;
	
	RESETn = 0;
	for(t1=0;t1<5000;t1++);			//... reset delay
	RESETn = 1;
	for(t1=0;t1<1000;t1++);			//... reset delay for SDRAM

}
//==================================================================================
//... initialize Interrupt
void Init_Interrupt()
{
#if 0
//... interrupt priority
//... PS = 0;	PT1 = 0;	PX1 = 0;	PT0 = 0;	PX0 = 0;
	IP = 0x00;

//... interrupt enable
//... EA = 0;	ES = 0;	ET1 = 0;	EX1 = 1;	ET0 = 0;	EX0 = 1;
	IE = 0x00;

	TMOD = 0x11;

//... interrupt type
//... IT: 1 (falling edge trigger), IT : 0 (low level trigger)
	IT0=1;
	IT1=1;

//... external interrupt (int3, int2)
/* XICON */
	PX3 = 0;		//... polarity
	EX3 = 0;		//... enable
	IE3 = 0;		//... status
	IT3 = 1;		//... type
	
	PX2 = 0;		//... polarity
	EX2 = 0;		//... enable
	IE2 = 0;		//... status
	IT2 = 1;		//... type

#else
//... interrupt type
//... IT: 1 (falling edge trigger), IT : 0 (low level trigger)
	IT0=1;
	IT1=1;

//... external interrupt (int3, int2)
/* XICON */
	PX3 = 0;		//... polarity
	EX3 = 0;		//... enable
	IE3 = 0;		//... status
	IT3 = 1;		//... type
	
	PX2 = 0;		//... polarity
	EX2 = 0;		//... enable
	IE2 = 0;		//... status
	IT2 = 1;		//... type

//... PCON(power control register)
	PCON = 0;	//... SMOD = 0;

//... SCON(serial port control register)
	SM0 = 0;
	SM1 = 1;		//... mode 1 of serial
	SM2 = 0;
	REN = 1;		//... receive enable
	TI = 0;
	RI = 0;	

//... interrupt priority
	IP = 0x00;

//... Timer Interrupt
	TMOD = 0x21;

	TH0 = 0xfe;
	TL0 = 0x00;

	TH1 = 0xf5;

//	TH1 = 0xa9;

	TF0 = 1;
	TR0 = 1;

	TR1 = 0;

//... interrupt enable
	IE = 0x87;	//disable serial interrupt
#endif
}
//==================================================================================
